<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="fn"  uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page import="kr.co.insoft.framework.config.Authorities" %>
<c:set var="CONTEXT">${pageContext.request.contextPath}</c:set>
<c:set var="cssUrl">${pageContext.request.contextPath}/resources/css</c:set>
<c:set var="jsUrl">${pageContext.request.contextPath}/resources/js</c:set>
<div class="col-sm-12 page-header custom-page-header">
	<h1><spring:message code="menu.template.application"/></h1>
	<small><spring:message code="menu.template.application.desc"/></small>
</div>
<form:form modelAttribute="applicationEntityModel" role="form" cssClass="form-horizontal">
	<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
	<input type="hidden" id="chkName" value="N"/>
	<!-- 어플리케이션 네임 -->
	<div class="form-group">
		<spring:message code="template.application.name.desc" var="msgNameDesc"/>
		<label class="col-sm-2 control-label"><spring:message code="template.application.name"/></label>
		<div class="col-sm-4">
			<form:input path="applicationName" cssClass="form-control" placeholder="${msgNameDesc}"/>
		</div>
		<div class="col-sm-2"> 
			<button class="btn btn-primary btnCheckName" type="button"><spring:message code="common.check.name"/></button>
		</div>
	</div>
	<!-- 어플리케이션 타입 -->
	<div class="form-group">
		<label class="col-sm-2 control-label"><spring:message code="template.application.type"/></label>
		<div class="col-sm-3">			
			<form:select path="applicationType" cssClass="form-control">
				<option value=""><spring:message code="select.seleted.message"/></option>
				<c:forEach items="${applicationTypeList }" var="list">
	            	<option value="${list.applicationType }">${list.applicationTypeName }</option>
	            </c:forEach>
			</form:select>
		</div>
	</div>
	<!-- 스크립트 선택 -->
	<div class="form-group">
		<label class="col-sm-2 control-label">Load Script</label>		
		<div class="col-sm-3">			
			<form:select path="scriptid" cssClass="form-control">
				<option value=""><spring:message code="select.seleted.message"/></option>
				<c:forEach items="${scriptList }" var="list">
	            	<option value="${list.scriptid }">${list.scriptName }</option>
	            </c:forEach>
			</form:select>	
		</div>
	</div>
	<!-- 스크립트 내용 -->
	<div class="form-group scriptGroup">
		<label class="col-sm-2 control-label">Install Script</label>
		<div class="col-sm-10"><form:textarea path="script"  cssClass="form-control" rows="15"/></div>
	</div>
	<!-- 요약 설명 -->
	<div class="form-group">
		<spring:message code="common.description.desc" var="msgDesc"/>
		<label class="col-sm-2 control-label"><spring:message code="common.description"/></label>
		<div class="col-sm-10"><form:textarea path="applicationDesc"  cssClass="form-control" rows="2" placeholder="${msgDesc}"/></div>
	</div>
	<!-- 하단 버튼 -->
	<div class="form-group">
		<div class="col-sm-12 text-right">
			<button class="btn btn-default btn_link" data-link="./lists" type="button"><spring:message code="button.list"/></button>
			<c:if test="${fn:contains(buttons , Authorities.CREATE)}">
			<button class="btn btn-primary createSubmitBtn" type="button"><spring:message code="button.save"/></button>
			</c:if>
		</div>
	</div>
</form:form>

<script type="text/javascript"><!--
$(document).ready(function() {
	/* 중복 네임 체크 */
	$(document).on("click", ".btnCheckName", function() {
		Loading.show();
		$.post("./isExistApplicationName", $("#applicationEntityModel").serialize() , function(res) {
		}).fail(function() {
			bootbox.alert("<spring:message code='common.server.fail'/>");
		}).done(function(res) {
			if (res) {
				bootbox.alert("<spring:message code='common.name.unavailable'/>");
			} else {
				bootbox.alert("<spring:message code='common.name.available'/>", function() {
					$("input[id='chkName']").val("Y");
				});
			}
		}).always(function() {
			Loading.hide();
		});
	})
	/* 스크립트 변경 */
	.on("change", "#scriptid", function() {
		bootbox.confirm("<spring:message code='template.script.change'/>", function(result) {
			if ( result ) {
				Loading.show();
				var param = {"ip" : "Y", "scriptid": $("#scriptid").val()};
				$.get("${CONTEXT}/template/script/view", param , function(res) {
				}).fail(function() {
					bootbox.alert("<spring:message code='common.server.fail'/>");
				}).done(function(res) {
					if ( res ) {
						$(".scriptGroup").html(res);
					}
				}).always(function() {
					Loading.hide();
				});
			}
		});
	})
	/* 어플리케이션 생성 */
	.on("click", ".createSubmitBtn", function() {
		if($("#chkName").val() == 'N'){
			bootbox.alert("<spring:message code='common.name.unavailable'/>");
			return;
		}
		bootbox.confirm("<spring:message code='common.save.confirm'/>", function(result) {
			if ( result ) {
				Loading.show();
				$.post("./createForm", $("#applicationEntityModel").serialize(), function(res) {
				}).fail(function() {
					bootbox.alert("<spring:message code='common.server.fail'/>");
				}).done(function(res) {
					if ( res ) {
						bootbox.alert("<spring:message code='common.save.ok'/>", function() {
							Loading.link("./lists");							
						});
					} else {
						bootbox.alert("<spring:message code='common.save.fail'/>");
					}
				}).always(function() {
					Loading.hide();
				});
			}
		});
	});
	/* 중복 네임 초기화 */
	$("#applicationName").bind('input keyup mouseup keydown mousedown', function() { 
		$("input[id='chkName']").val("N");
	});
});
//--></script>